package com.imooc.mall.controller;

import com.github.pagehelper.PageInfo;
import com.imooc.mall.common.ApiRestResponse;
import com.imooc.mall.model.request.CreateOrder;
import com.imooc.mall.model.vo.OrderVO;
import com.imooc.mall.service.OrderService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@RestController
public class OrderController {

    @Autowired
    private OrderService orderService;

    // 下单入参 只需要收件人的信息 我们会去用户中查询商品的信息
    @PostMapping("order/create")
    @ApiOperation("创建订单")
    public ApiRestResponse create (@RequestBody @Valid CreateOrder createOrder){
        String orderNo = orderService.create(createOrder);
        return ApiRestResponse.success(orderNo);
    }

    @GetMapping("order/detail")
    @ApiOperation("订单详情")
    public ApiRestResponse detail(@RequestParam String orderNo){
        OrderVO detail = orderService.detail(orderNo);
        return ApiRestResponse.success(detail);
    }

    @GetMapping("order/list")
    @ApiOperation("前台订单列表")
    public ApiRestResponse list(@RequestParam Integer pageNum, @RequestParam Integer pageSize){
        PageInfo list = orderService.list(pageNum, pageSize);
        return ApiRestResponse.success(list);
    }


    @PostMapping("order/cancel")
    @ApiOperation("前台订单取消列表")
    public ApiRestResponse list(@RequestParam String orderNo){
        orderService.cancel(orderNo);
        return ApiRestResponse.success();
    }


    @GetMapping("order/qrcode")
    @ApiOperation("生成支付二维码")
    public ApiRestResponse qrcode(@RequestParam String orderNo){
        String qrcode = orderService.qrcode(orderNo);
        return ApiRestResponse.success(qrcode);
    }

    @PostMapping("pay")
    @ApiOperation("支付接口")
    public ApiRestResponse pay(@RequestParam String orderNo){
        orderService.pay(orderNo);
        return ApiRestResponse.success();
    }
}